package com.feihong.yw189.bluetooth.utils.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.util.Base64;
import android.util.Log;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

/**
 * Created by Admin on 2017/2/14.
 */
//身份证照片解码类
public  class BmpDecrypt implements Runnable
{
    private static final String TAG = BmpDecrypt.class.getName();
    String m_url;
    String m_sn;
    String m_token;
    byte[] m_data;
    Handler m_handler;
    BmpDecrypt(String imgUrl, String sn, String token, byte[] data, Handler handler)
    {
        m_url = imgUrl;
        m_sn = sn;
        m_token = token;
        m_data = data;
        m_handler = handler;
    }
    @Override
    public void run() {
        BmpDecryptRemote(m_url, m_sn, m_token, m_data, m_handler);
    }
    //身份证照片远程解码
    static Bitmap BmpDecryptRemote(String strURL, String strSN, String strToken, byte[] data) {
        return BmpDecryptRemote(strURL, strSN, strToken, Base64.encodeToString(data, Base64.DEFAULT));
    }

    //身份证照片远程解码
   public static Bitmap BmpDecryptRemote(String strURL, String strSN, String strToken, String strWLTBase64) {
        String strPost = null;
        Bitmap bitmap = null;
        try {
            strPost = "ClientSN=" + URLEncoder.encode(strSN, "utf-8") + "&Token=" + URLEncoder.encode(strToken, "utf-8") + "&Wlt=" + URLEncoder.encode(strWLTBase64, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        byte[] postResponse = RequestToUrl(strURL, "POST", strPost.getBytes());
        if (postResponse != null) {
            String strJpgBase64 = new String(postResponse);
            if (strJpgBase64 != null) {
                byte[] jpgByte = Base64.decode(strJpgBase64, Base64.DEFAULT);
                if (jpgByte != null)
                    bitmap = BitmapFactory.decodeByteArray(jpgByte, 0, jpgByte.length);
            }
        }
        return bitmap;
    }

    //发送图片编码信息至服务器，并将返回值返回给handler
    static void BmpDecryptRemote(String strURL, String sn, String token, byte[] data, Handler handler) {
        Bitmap bitmap = BmpDecryptRemote(strURL, sn, token, data);
        if (bitmap != null) {
            Message msg = handler.obtainMessage(0, bitmap);
            msg.sendToTarget();
        }
    }
    /**
     * 到Url地址上去下载数据，并传数据回來
     *
     * @param strURL * @return
     */
    public static byte[] RequestToUrl(String strURL, String strMethod, byte[] postData)
    {
        byte[] responseBuff = null;
        try {
            URL url = new URL(strURL);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setRequestMethod(strMethod);
            conn.setDoInput(true);
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);
            if (postData != null) {
                conn.setDoOutput(true);
                OutputStream os = conn.getOutputStream();
                os.write(postData);
                os.flush();
                os.close();
            }
            int nStatusCode = conn.getResponseCode();
            Log.i(TAG, "RequestToUrl: " + "StatusCode="+nStatusCode);
            if(nStatusCode == 200){
                InputStream is =conn.getInputStream();
                BufferedInputStream bis = new BufferedInputStream(is);
                responseBuff = getBytes(is);
                bis.close();
                return responseBuff;
            }
        }catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将InputStream对象转换为Byte[]
     * @param is
     * @return
     * @throws IOException */

    public static byte[] getBytes(InputStream is) throws IOException {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] b = new byte[1024];
        int len = 0;
        while ((len = is.read(b, 0, 1024)) != -1)
        {
            baos.write(b, 0, len);
            baos.flush();
        }
        byte[] bytes = baos.toByteArray();
        return bytes;
    }
}
